package com.stock.demo.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.stock.demo.adapter.viewholder.StockViewHolder;
import com.stock.demo.model.MarketViewBody;
import com.stock.demo.model.MarketViewFoot;
import com.stock.demo.model.MarketViewHead;
import com.stock.demo.model.MarketViewType;

import java.util.List;

/**
 * Created by XuZhaoyu on 2017/6/26.
 */

public class MarketViewAdapter extends RecyclerView.Adapter<StockViewHolder> {

    private static final int TYPE_HEAD = 0;//大盘指数栏目
    private static final int TYPE_BODY = 1;//热门行业和热门概念栏目
    private static final int TYPE_FOOT = 2;//其他栏目

    private List<MarketViewType> mData;
    private AdapterBindViewListener mBindViewListener;
    private int mHeadViewResId;
    private int mBodyLayoutResId;
    private int mFootLayoutResId;

    public MarketViewAdapter(List<MarketViewType> data, int headViewResId, int bodyLayoutResId, int footLayoutResId, AdapterBindViewListener bindViewListener) {
        mData = data;
        mHeadViewResId = headViewResId;
        mBodyLayoutResId = bodyLayoutResId;
        mFootLayoutResId = footLayoutResId;
        mBindViewListener = bindViewListener;
    }

    @Override
    public StockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_HEAD:
                view = LayoutInflater.from(parent.getContext()).inflate(mHeadViewResId, parent, false);
                break;
            case TYPE_BODY:
                view = LayoutInflater.from(parent.getContext()).inflate(mBodyLayoutResId, parent, false);
                break;
            case TYPE_FOOT:
                view = LayoutInflater.from(parent.getContext()).inflate(mFootLayoutResId, parent, false);
                break;
        }
        return new StockViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final StockViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEAD:
                mBindViewListener.bindHeadView(holder, position);
                break;
            case TYPE_BODY:
                mBindViewListener.bindBodyView(holder, position);
                break;
            case TYPE_FOOT:
                mBindViewListener.bindFootView(holder, position);
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mData.get(position) instanceof MarketViewHead) {
            return TYPE_HEAD;
        }
        if (mData.get(position) instanceof MarketViewBody) {
            return TYPE_BODY;
        }
        if (mData.get(position) instanceof MarketViewFoot) {
            return TYPE_FOOT;
        }
        return TYPE_BODY;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public interface AdapterBindViewListener {
        void bindHeadView(StockViewHolder holder, int position);

        void bindBodyView(StockViewHolder holder, int position);

        void bindFootView(StockViewHolder holder, int position);
    }

}
